#line 2 "F:\Emergent\Gamebryo-2.6-Source\Samples\Demos\TerrainDemo\Data\Shaders\Generated\NiWaterMaterial\00000023-000000C1-00000000-00000000-P.hlsl"
/*
Shader description:
TANGENTS = 1
ENABLE_SUNLIGHT = 1
USE_PLANAR_REFLECTION = 0
USE_CUBE_MAP_REFLECTION = 0
NUM_TEXCOORDS = 1
REFRACTION_MAP = 0
REFLECTION_MAP = 0
NORMAL_MAP = 1
*/

//---------------------------------------------------------------------------
// Types:
#if defined(DIRECT3D)
#define BONEMATRIX_TYPE float4x3
#else
#define BONEMATRIX_TYPE float3x4
#endif
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Constant variables:
//---------------------------------------------------------------------------

sampler2D Base;
sampler2D Normal;
float4 g_LightWorldDirection0;
float4 g_LightDiffuse0;
float4 g_LightSpecular0;
float Specularity;
float FogDensity;
float FogDistance;
float FogFallOff;
float4 g_FogDensity;
float4 g_FogNearFar;
float4 g_FogColor;
//---------------------------------------------------------------------------
// Functions:
//---------------------------------------------------------------------------

/*

      This fragment is responsible for sampling a texture and returning its value
      as a RGB value and an A value.
    
*/

void TextureRGBASample(float2 TexCoord,
    sampler2D Sampler,
    bool Saturate,
    out float4 ColorOut)
{

      ColorOut = tex2D(Sampler, TexCoord);
      if (Saturate)
      {
      ColorOut = saturate(ColorOut);
      }
    
}
//---------------------------------------------------------------------------
/*

      Returns the alpha value of a given color

    
*/

void RetrieveAlpha(float4 Color,
    out float AlphaOut,
    out float3 ColorOut)
{

     
      AlphaOut = Color.a;
      ColorOut.rgb = Color.rgb;
    
}
//---------------------------------------------------------------------------
/*

      Calculates the fog factor. This is used to compute the refraction

    
*/

void ComputeFogFactor(float ViewDistance,
    float3 ViewVector,
    float3 WorldNormal,
    float Depth,
    float FogDensity,
    float FogDistance,
    float FogFallOff,
    out float FogFactor)
{

      float distanceFactor = pow(clamp(ViewDistance / FogDistance, 0, 1), FogFallOff);
      FogFactor = 1 - dot(ViewVector, WorldNormal);
      FogFactor = clamp(FogFactor * FogFactor,0,1);
      FogFactor = FogFactor + distanceFactor;
      FogFactor = FogFactor * pow(Depth, 0.5);
      FogFactor = clamp(FogFactor,0,1);
      FogFactor = clamp((FogFactor * FogDensity), 0, 1);
    
}
//---------------------------------------------------------------------------
/*

      This fragment is responsible for handling fogging calculations.
      FogType can be one of 4 values:

      NONE   - 0
      EXP    - 1
      EXP2   - 2
      LINEAR - 3
    
*/

void CalculateFog(int FogType,
    float FogDensity,
    float ViewDistance,
    bool FogRange,
    float2 FogStartEnd,
    out float FogOut)
{

      if (FogType == 0) // NONE
      {
      FogOut = 1.0;
      }
      else if (FogType == 1) // EXP
      {
      FogOut = 1.0 / exp( ViewDistance * FogDensity);
      }
      else if (FogType == 2) // EXP2
      {
      FogOut = 1.0 / exp( pow( ViewDistance * FogDensity, 2));
      }
      else if (FogType == 3) // LINEAR
      {
      FogOut = saturate((FogStartEnd.y - ViewDistance) /
      (FogStartEnd.y - FogStartEnd.x));
      }
    
}
//---------------------------------------------------------------------------
/*

      This fragment is responsible for computing the final RGBA color.
    
*/

void CalculateWorldNormal(float3 Normal,
    float3 Tangent,
    float3 Binormal,
    float4 NormalMap,
    out float3 WorldNormalOut)
{

      NormalMap.rgb = normalize((2 * (NormalMap.rgb)) - 1);

      float3 Nn = normalize(Normal);
      float3 Tn = normalize(Tangent);
      float3 Bn = normalize(Binormal);
      Nn = Nn + NormalMap.r*Tn + NormalMap.g*Bn;
      WorldNormalOut = normalize(Nn);

    
}
//---------------------------------------------------------------------------
/*

      Calculates the reflection vector to be used for cubemap reflection

    
*/

void CalculateReflectionVector(float3 ViewVector,
    float3 Normal,
    out float3 ReflectionVector)
{


      ReflectionVector = normalize(reflect(ViewVector, Normal));
    
}
//---------------------------------------------------------------------------
/*

      Accumulates the affects of a single light.

      WorldNormal and LightDirection are assumed to be normalized.
    
*/

void CalculateDiffuseColor(float3 Normal,
    float3 LightDirection,
    float3 LightDiffuse,
    out float3 DiffuseOut)
{


      float NdotL = saturate(dot(Normal, -LightDirection));
      DiffuseOut = LightDiffuse * NdotL;
    
}
//---------------------------------------------------------------------------
/*

      Multiplies two colors together

    
*/

void ModulateColors(float3 MaterialColor,
    float3 LightDiffuse,
    out float3 DiffuseOut)
{


      DiffuseOut.rgb = saturate(MaterialColor.rgb * LightDiffuse);
    
}
//---------------------------------------------------------------------------
/*

      Calculates the specular color

    
*/

void CalculateSpecularColor(float3 ReflectionVector,
    float3 LightDirection,
    float3 LightSpecular,
    float Specularity,
    out float3 ColorOut)
{

      ColorOut = 2 * LightSpecular * pow(dot(ReflectionVector, LightDirection), Specularity);
    
}
//---------------------------------------------------------------------------
/*

      Additions two colors together
     
    
*/

void AccumulateColors(float3 MaterialColor,
    float3 LightDiffuse,
    out float3 DiffuseOut)
{


      DiffuseOut.rgb = saturate(MaterialColor.rgb + LightDiffuse);
    
}
//---------------------------------------------------------------------------
/*

      This fragment is responsible for computing the final RGBA color.
    
*/

void SetColorAndAlpha(float3 Color,
    float Alpha,
    out float4 OutputColor)
{

      OutputColor.rgb = Color.rgb;
      OutputColor.a = Alpha;
    
}
//---------------------------------------------------------------------------
/*

      This fragment is responsible for applying the fog amount.
    
*/

void ApplyFog(float4 UnfoggedColor,
    float3 FogColor,
    float FogAmount,
    out float4 FoggedColor)
{

      FoggedColor.rgb = lerp(FogColor, UnfoggedColor.rgb, FogAmount);
      FoggedColor.a = UnfoggedColor.a;
    
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Input:
//---------------------------------------------------------------------------

struct Input
{
    float4 PosProjected : POSITION0;
    float4 WorldPosition : TEXCOORD0;
    float3 WorldView : TEXCOORD1;
    float ViewDistance : TEXCOORD2;
    float3 WorldNormal : TEXCOORD3;
    float3 WorldTangent : TEXCOORD4;
    float3 WorldBinormal : TEXCOORD5;
    float2 BaseUV : TEXCOORD6;
    float2 NormalUV : TEXCOORD7;

};

//---------------------------------------------------------------------------
// Output:
//---------------------------------------------------------------------------

struct Output
{
    float4 Color0 : COLOR0;

};

//---------------------------------------------------------------------------
// Main():
//---------------------------------------------------------------------------

Output Main(Input In)
{
    Output Out;
	// Function call #0
    float4 ColorOut_CallOut0;
    TextureRGBASample(In.BaseUV, Base, bool(false), ColorOut_CallOut0);

	// Function call #1
    float AlphaOut_CallOut1;
    float3 ColorOut_CallOut1;
    RetrieveAlpha(ColorOut_CallOut0, AlphaOut_CallOut1, ColorOut_CallOut1);

	// Function call #2
    float FogFactor_CallOut2;
    ComputeFogFactor(In.ViewDistance, In.WorldView, In.WorldNormal, 
        AlphaOut_CallOut1, FogDensity, FogDistance, FogFallOff, 
        FogFactor_CallOut2);

	// Function call #3
    float FogOut_CallOut3;
    CalculateFog(int(3), g_FogDensity, In.ViewDistance, bool(true), 
        g_FogNearFar, FogOut_CallOut3);

	// Function call #4
    float4 ColorOut_CallOut4;
    TextureRGBASample(In.NormalUV, Normal, bool(false), ColorOut_CallOut4);

	// Function call #5
    float3 WorldNormalOut_CallOut5;
    CalculateWorldNormal(In.WorldNormal, In.WorldTangent, In.WorldBinormal, 
        ColorOut_CallOut4, WorldNormalOut_CallOut5);

	// Function call #6
    float3 ReflectionVector_CallOut6;
    CalculateReflectionVector(In.WorldView, WorldNormalOut_CallOut5, 
        ReflectionVector_CallOut6);

	// Function call #7
    float3 DiffuseOut_CallOut7;
    CalculateDiffuseColor(WorldNormalOut_CallOut5, g_LightWorldDirection0, 
        g_LightDiffuse0, DiffuseOut_CallOut7);

	// Function call #8
    float3 DiffuseOut_CallOut8;
    ModulateColors(ColorOut_CallOut1, DiffuseOut_CallOut7, DiffuseOut_CallOut8);

	// Function call #9
    float3 ColorOut_CallOut9;
    CalculateSpecularColor(ReflectionVector_CallOut6, g_LightWorldDirection0, 
        g_LightSpecular0, Specularity, ColorOut_CallOut9);

	// Function call #10
    float3 DiffuseOut_CallOut10;
    AccumulateColors(DiffuseOut_CallOut8, ColorOut_CallOut9, 
        DiffuseOut_CallOut10);

	// Function call #11
    float4 OutputColor_CallOut11;
    SetColorAndAlpha(DiffuseOut_CallOut10, AlphaOut_CallOut1, 
        OutputColor_CallOut11);

	// Function call #12
    ApplyFog(OutputColor_CallOut11, g_FogColor, FogOut_CallOut3, Out.Color0);

    return Out;
}

